<?php 
/* ================================================================================
 * LWAdmin	 
 *
 * ministries.php: This is the ministries file for Living Web.
 *
 * Author: Eric Hendrickson (enhendrickson@liberty.edu)
 * Date: 6/26/05
   ================================================================================ */
	
	global $SSO, $Security, $AppID, $User, $DB;
	//Check to see if there is a valid session 
	$SessionInfo = $SSO->isValidSession("index.php?action=login", $SSO->getSessionInformation());	
	
	//Check to see if the user has rights to be on LWAdmin
	$PageRights = array('ADMIN', 'MINIADMIN');
	$Security->ValidateUser($SessionInfo['username'], $AppID, $PageRights, 1);
	
	//Grab the user info
	$UserInfo = $User->GetUserInfo($SessionInfo['username']);
?>


<table cellpadding="0" cellspacing="0" border="0">
	<tr>
		<td>
			<?php			
				if(!isset($_GET['a']))
				{
					$_GET['a'] = '';
				}

				switch($_GET['a'])
				{
					case 'save':
					{
						global $DB;
						if(isset($_POST['txtMinistryID']))
						{
							$DB->query("SELECT * FROM ministries WHERE MinistryID = '" . $_POST['txtMinistryID'] . "' ORDER BY MinistryName");
							
							if($row = $DB->fetch_row())
							{					
								$db_string = $DB->compile_db_update_string( 
								array (
									'MinistryName'		=> str_replace("\\","",$_POST['txtMinistryName']),
									'MinistryHead'   		=> $_POST['selMinistryHead'],
									'MinistryDesc'    		=> $_POST['txtMinistryDesc'],
									'MinistryPred'    		=> $_POST['txtMinistryPred'])
									);
								
								//insert the session into the sessions table
								$DB->query("UPDATE ministries SET " . $db_string . " WHERE MinistryID = '" . $_POST['txtMinistryID'] . "'");
							}
							else
							{
								$db_string = $DB->compile_db_insert_string( 
								array (
									'MinistryID'		=> $_POST['txtMinistryID'],
									'MinistryName'		=> str_replace("\\","",$_POST['txtMinistryName']),
									'MinistryHead'   		=> $_POST['selMinistryHead'],
									'MinistryDesc'    		=> $_POST['txtMinistryDesc'],
									'MinistryPred'    		=> $_POST['txtMinistryPred'])
									);
								
								$DB->query("INSERT INTO ministries (".$db_string['FIELD_NAMES'].") VALUES (".$db_string['FIELD_VALUES'].")");
							}
							
							header("Location: index.php?action=ministries");
						}
						else
						{
							print "There was an error please contact your administrator.";
						}
						break;
					}
					case 'edit':
					{
						if(isset($_POST['selMinistries']))
						{
							MinistryForm($_POST['selMinistries']);
						}
						else
						{
							header("Location: index.php?action=ministries");
						}
						break;
					}
					case 'delete':
					{
						if(isset($_POST['selMinistries']))
						{
							DeleteMinistry($_POST['selMinistries']);
						}
						
						header("Location: index.php?action=ministries");
						break;
					}
					case 'new':
					{
						if(isset($_POST['selMinistries']))
						{
							MinistryForm('');
						}
						else
						{
							header("Location: index.php?action=ministries");
						}
						break;
					}
					default:
					{
						print '<form action="index.php?action=ministries" method="post" name="frmMinstries">';
						ChooseMinistry();						
						print '</form>';
						break;
					}
				}
			?>
		</td>
	</tr>
</table>

<?php
	function ChooseMinistry()
	{
		print <<<HTML
		<table style="border-spacing:0px;border:0px;padding:5px;width:95%;">
				<tr>
					<td align="center">
						<table style="border-spacing:0px;border:0px;padding:0px;">
							<tr>
								<td>
									<span class="TB1" style="color:#555555;">Ministry List</span>
								</td>
							</tr>
							<tr>
								<td valign="middle" >
									<select name="selMinistries"  class="T1" multiple size="10">
HTML;
			print GenerateMinistryOptions();
			print <<<HTML
								 </select> 
								</td>
							</tr>
							<tr>
								<td><span class="T1" style="font-size:10px;">Select Only One</span>
								</td>
							</tr>
							<tr>
								<td valign="middle">
									<table cellpadding="0" cellspacing="0">
										<tr>
											<td>
												<table cellpadding="0" cellspacing="0">
													<tr>
														<td>
															<input type="button" class="B" value="New" onClick="this.form.action='index.php?action=ministries&a=new';this.form.submit();"> 
														</td>
														<td>
															&nbsp;&nbsp;&nbsp;</td>
														<td>
															<input type="button" class="B" value="Edit" onClick="this.form.action='index.php?action=ministries&a=edit';this.form.submit();"> 
														</td>
														<td>
															&nbsp;&nbsp;&nbsp;</td>
														<td>
															<input type="button" class="B" value="Delete" onClick="if(confirm('Are you sure you would like to delete this ministry?')){this.form.action='index.php?action=ministries&a=delete';this.form.submit();}"> 
														</td>
													</tr>
												</table>
											</td>
										</tr>
									</table>
								</td>
							</tr>
						</table>
					</td>
				</tr>
			</table>
HTML;
	}

	function GenerateMinistryOptions()
	{
		global $DB;
		$Options = "";
		$DB->query("SELECT * FROM ministries ORDER BY MinistryName");
		$row = $DB->fetch_row();
		$Options = $Options . '<option selected value="' . $row['MinistryID'] . '">' . $row['MinistryName'] . '</option>';
		while($row = $DB->fetch_row())
		{	
			$Options = $Options . '<option value="' . $row['MinistryID'] . '">' . $row['MinistryName'] . '</option>';
		}
		
		return $Options;
	}
	
	function DeleteMinistry($ministry_id)
	{
		global $DB;
		$DB->query("DELETE FROM ministries WHERE MinistryID = '" . $ministry_id . "'");
		return;
	}

	function MinistryForm($ministry_id)
	{
		global $DB;
		
		if($ministry_id != "")
		{
			$DB->query("SELECT * FROM ministries WHERE MinistryID = '" . $ministry_id . "' ORDER BY MinistryName");
		}
				
		if($row = $DB->fetch_row())
		{
			$ministry_name = $row['MinistryName'];
			$ministry_head = $row['MinistryHead'];
			$ministry_desc = $row['MinistryDesc'];
			$ministry_pred = $row['MinistryPred'];
		}
		else
		{
			$ministry_name = "";
			$ministry_head = "";
			$ministry_desc = "";
			$ministry_pred = "";
		}
		
		//select out the users and put them in a drop down box
		$DB->query("SELECT Username, CONCAT(LastName, CONCAT(', ',CONCAT(FirstName , CONCAT(' ',MiddleName)))) as FullName FROM users ORDER BY FullName");
		
		$users_select_box = "";
		while($row = $DB->fetch_row())
		{
			$users_select_box = $users_select_box . '<option class="T1" value="' . $row['Username'] . '"'; 
			if($ministry_head == $row['Username'])
			{
				$users_select_box = $users_select_box . ' selected ';
			}
			$users_select_box = $users_select_box . '>' . $row['FullName'] . '</option>';
		} 
				
		print <<<HTML
		<form name="frmMinistry" action="index.php?action=ministries&a=save" method="post">
		<table cellspacing="0" cellpadding="0" align="center">
			<tr>
				<td>
					<br><span class="TB1" style="color:#555555;">Ministry Information</span><br><br>
				</td>
			</tr>
			<tr>
				<td>
						<span class="T1">Ministry ID</span>&nbsp;
						<input name="txtMinistryID" class="T1" type="text" value="$ministry_id"><br><br>
						<span class="T1">Ministry Name</span>&nbsp;
						<input name="txtMinistryName" class="T1" type="text" value="$ministry_name"><br><br>
						<span class="T1">Ministry Predecessor</span>&nbsp;
						<input name="txtMinistryPred" class="T1" type="text" value="$ministry_pred"><br><br>
						<span class="T1">Ministry Head</span>&nbsp;
						<select name="selMinistryHead" class="T1">$users_select_box</select><br><br>
						<span class="T1">Ministry Description</span><br>
						<textarea cols="30" rows="3" name="txtMinistryDesc" class="T1">$ministry_desc</textarea><br><br>
				</td>
			</tr>
			<tr>
				<td>
					<input name="btnSave" value="Save" class="B" type="submit">&nbsp;&nbsp;
					<input name="btnCancel" value="Cancel" class="B" type="button" onClick="location.href='index.php?action=ministries';">
				</td>
			</tr>
		</table>
		</form>
HTML;
		return;
	}
?>
